/******************************************************************************* 
 * Copyright (C) 2012-2015 Microfountain Technology, Inc. All Rights Reserved. 
 * 
 * Unauthorized copying of this file, via any medium is strictly prohibited.   
 * Proprietary and confidential
 * 
 * Last Modified: 2015-9-22 17:43:21
 ******************************************************************************/
package cn.com.xy.sms.util;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.content.Context;
import cn.com.xy.sms.sdk.constant.Constant;
import cn.com.xy.sms.sdk.dex.DexUtil;
import cn.com.xy.sms.sdk.log.LogManager;
import cn.com.xy.sms.sdk.net.util.SigntureUtil;
import cn.com.xy.sms.sdk.queue.SdkTaskQueue;
import cn.com.xy.sms.sdk.queue.TaskInfo;
import cn.com.xy.sms.sdk.util.DuoquUtils;
import cn.com.xy.sms.sdk.util.RecordFunctionUtil;

public class ParseOnePlusCardManager {

    /**
     * 解析短信创建卡片
     * 
     * @param ctx
     *            上下文
     * @param phoneNumber
     *            短信接入码即短信发送方号码
     * @param smsCenterNum
     *            短信中心号码,短信发送方的短信中心.接收短信时可以解析到此中心号码值
     * @param smsContent
     *            短信内容
     * @param extend
     * @return Map key:Result value false或true
     */
    public static Map<String, Object> parseMsgForCard(Context ctx, String phoneNumber, String smsCenterNum, String smsContent, Map<String, String> extend) throws Exception {
        if (ctx == null) {
            throw new Exception(" Context is null.");
        }

        if (phoneNumber == null) {
            throw new Exception(" phoneNumber is null.");
        }
        if (smsContent == null) {
            throw new Exception(" smsContent is null.");
        }
        /*SDK-707 hewengao/20161226 start*/
        if(extend == null){
            extend = new HashMap<String, String>();
        }
        if (!extend.containsKey("PARSE_TIME_OUT")) {
            extend.put("PARSE_TIME_OUT", String.valueOf(15000));
        }
        /*SDK-707 hewengao/20161226 end*/
        Map<String, Object> map = null;
        if (SigntureUtil.checkChannelFuncPower(Constant.POWER_TO_YUNCARD)) {

            Map<String, Object> res = ParseManager.parseMsg(ctx, phoneNumber, smsCenterNum, smsContent, 0l, extend);
            if (res != null && ParseBubbleManager.getParseStatu(res) != -1) {
                res.put("msgTime", extend.get("msgTime"));
                map = DexUtil.handerValueMap(res);
                if (map == null) {
                    map = new HashMap<String, Object>();
                    map.put("Result", false);
                    return map;
                }
                SdkTaskQueue.addTask(new TaskInfo(12, "state", String.valueOf(RecordFunctionUtil.CARD_STATE)));//卡夹的标记记录
                SdkTaskQueue.addTask(new TaskInfo(12, "state",  String.valueOf(RecordFunctionUtil.CARD_STATE)));//卡夹的状态标记
                // Object obj = map.get("card_content");
                JSONObject jsData = (JSONObject) map.get("card_content");
                if (jsData != null) {
                    Integer cardType = (Integer) jsData.get("card_type");
                    JSONObject resultJson = new JSONObject();
                    resultJson.put("msgid", extend.get("msgid"));
                    resultJson.put("phone", extend.get("phone"));
                    resultJson.put("content", extend.get("content"));
                    resultJson.put("msgTime", extend.get("msgTime"));
                    resultJson.put("smsdate", extend.get("smsdate"));
                    jsData.put("msgid", extend.get("msgid"));
                    jsData.put("smsdate", extend.get("smsdate"));
                    resultJson.put("result", jsData.toString());
                    long id = DuoquUtils.getSdkDoAction().createCard(resultJson, cardType, extend);
                    map.put("Result", id > 0);
                    map.put("id", id + "");
                    return map;
                }
            }
        }
        if (map == null) {
            map = new HashMap<String, Object>();
            map.put("Result", false);
        }
        return map;

    }
    
    /**
     * 解析短信创建卡片(一加卡夹2.0)
     * 
     * @param ctx
     *            上下文
     * @param phoneNumber
     *            短信接入码即短信发送方号码
     * @param smsCenterNum
     *            短信中心号码,短信发送方的短信中心.接收短信时可以解析到此中心号码值
     * @param smsContent
     *            短信内容
     * @param extend
     * @return Map key:Result value false或true
     */
    public static synchronized Map<String, Object> parseMsgForCardNew(Context ctx, String phoneNumber, String smsCenterNum, String smsContent, Map<String, String> extend) throws Exception {
        if (ctx == null) {
            throw new Exception(" Context is null.");
        }

        if (phoneNumber == null) {
            throw new Exception(" phoneNumber is null.");
        }
        if (smsContent == null) {
            throw new Exception(" smsContent is null.");
        }
         /*SDK-707 hewengao/20161226 start*/
        if(extend == null){
            extend = new HashMap<String, String>();
        }
        if (!extend.containsKey("PARSE_TIME_OUT")) {
            extend.put("PARSE_TIME_OUT", String.valueOf(15000));
        }
        /*SDK-707 hewengao/20161226 end*/
        Map<String, Object> map = null;
        if (SigntureUtil.checkChannelFuncPower(Constant.POWER_TO_YUNCARD)) {

            Map<String, Object> res = ParseManager.parseMsg(ctx, phoneNumber, smsCenterNum, smsContent, 0l, extend);
            if (res != null && ParseBubbleManager.getParseStatu(res) != -1) {
                res.put("msgTime", extend.get("msgTime"));
                map = DexUtil.handerValueMap(res);
                if (map == null) {
                    map = new HashMap<String, Object>();
                    map.put("Result", false);
                    return map;
                }
                
                SdkTaskQueue.addTask(new TaskInfo(12, "state", String.valueOf(RecordFunctionUtil.CARD_STATE)));//卡夹的标记记录
                SdkTaskQueue.addTask(new TaskInfo(12, "state",  String.valueOf(RecordFunctionUtil.CARD_STATE)));//卡夹的状态标记

                String titleNum = map.get("title_num").toString().substring(0,5);
                Integer cardType = 0;
                if(map.get("card_type") instanceof Integer){
                    cardType = (Integer) map.get("card_type");
                }else{
                    cardType = Integer.parseInt((String) map.get("card_type"));
                }
                JSONObject resultJson = new JSONObject();
                resultJson.put("msgid", extend.get("msgid"));
                resultJson.put("phone", extend.get("phone"));
                resultJson.put("content", extend.get("content"));
                resultJson.put("msgTime", extend.get("msgTime"));
                resultJson.put("smsdate", extend.get("smsdate"));
                map.put("msgid", extend.get("msgid"));
                map.put("smsdate", extend.get("smsdate"));
                resultJson.put("result", (new JSONObject(map)).toString());
                resultJson.put("title_num", titleNum);
                resultJson.put("card_key_index1", map.get("card_key_index1"));
                resultJson.put("card_key_index2", map.get("card_key_index2"));
                long id = DuoquUtils.getSdkDoAction().createCard(resultJson, cardType, extend);
                map.put("Result", id > 0);
                map.put("id", id + "");
                return map;
            }
        }
        return map;
    }
}
